package part18;

import java.nio.ByteBuffer;

/**
 * @author wcj
 * @Date 2021/6/4 15:52
 * @Version 1.0
 */
public class Part1823 {

    private static final int SIZE = 1024;

    /**
     * ByteBuffer虽然只能放入字节类型的数据，但是我们可以通过buffer.asLongBuffer()等方法放入基本类型的数据
     *
     * @param args
     */
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(SIZE);
        int i = 0;
        while (i++ < buffer.limit()) {
            if (buffer.get() != 0) {
                System.out.println("non zero");
            }
        }

        System.out.println("i= " + i);
        buffer.rewind();
        buffer.asCharBuffer().put("Howdy!");
        char c;
        while ((c = buffer.getChar()) != 0) {
            System.out.println(c + " ");
        }

        buffer.rewind();
        buffer.asShortBuffer().put((short) 471142);
        System.out.println(buffer.getShort());

        buffer.rewind();
        buffer.asIntBuffer().put(9975462);
        System.out.println(buffer.getInt());

        buffer.rewind();
        buffer.asLongBuffer().put(84684228);
        System.out.println(buffer.getLong());
    }
}
